package com.qifei.xmlg_backend.controller;

import com.qifei.xmlg_backend.DTO.classes.ClassStudentCountVO;
import com.qifei.xmlg_backend.entity.Class;
import com.qifei.xmlg_backend.entity.Result;
import com.qifei.xmlg_backend.service.IClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 *
 * 班级
 *
 * @author qifei
 * @since 2025-06-11
 */
@RestController
@RequestMapping("/classes")
public class ClassController {

    @Autowired
    private IClassService classService;

    @GetMapping()
    public Result<List> getAll(){
        List<Class> list = classService.list();
        return Result.success(list);
    }

    @PostMapping()
    public Result<Void> add(@RequestBody Class c){
        boolean success = classService.save(c);
        if (success) {
            return Result.success();
        }
        return Result.error("创建班级失败");
    }
    @PutMapping()
    public Result<Void> put(@RequestBody Class c){
        boolean success = classService.updateById(c);
        if (success) {
            return Result.success();
        }
        return Result.error("更新班级失败");
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable  int id){
        boolean success = classService.removeById(id);
        if (success) {
            return Result.success();
        }
        return Result.error("删除班级失败");
    }

    /**
     *
     * 获取每个班级人数
     */
    @GetMapping("/students/counts")
    public Result<?> getStudentCountByClassId() {
        List<ClassStudentCountVO> ClassStudentCountVO = classService.getStudentCountByClassId();
        return Result.success(ClassStudentCountVO);
    }
}
